﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Ascii_Art
{
    public partial class frmMain : Form
    {
        #region Fields
        //Numero degli Shades
        const int MAX_SHADES = 10;
        //Shades
        char[] shades = new char[] { '#', '$', 'O', '=', '+', '|', '-', '^', '.', ' ' };
        #endregion

        #region Methods
        public frmMain()
        {
            InitializeComponent();
        }

        String Compute(Image source)
        {
            String toRet = "";
            try
            {
                cmdExport.Visible = false;
                prgBar.Visible = true;
                //riduco l'immagine alla larghezza massima di 20 caratteri
                //width : height = 20 : x
                float width = (float)nudWidth.Value;
                float height = (float)pbImage.Image.Height / (float)pbImage.Image.Width * width;
                prgBar.Maximum = (int)height;
                prgBar.Step = 1;

                using (Bitmap image = new Bitmap(source.GetThumbnailImage((int)width, (int)height, null, IntPtr.Zero)))
                {
                    //per ogni pixel
                    for (int y = 0; y < image.Height; y++)
                    {
                        for (int x = 0; x < image.Width; x++)
                        {
                            //determina il colore medio
                            Color pixel = image.GetPixel(x, y);
                            int averange_color = (pixel.R + pixel.G + pixel.B) / 3;
                            averange_color /= (256 / MAX_SHADES);
                            if (averange_color >= MAX_SHADES)
                                averange_color--;
                            toRet += shades[averange_color];
                        }
                        //aggiorna barra del progresso
                        toRet += Environment.NewLine;
                        prgBar.PerformStep();
                        Application.DoEvents();
                    }
                    cmdExport.Visible = true;
                    prgBar.Visible = false;
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
            return toRet;
        }

        private void cmdExport_Click(object sender, EventArgs e)
        {
            try
            {
                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    sfd.Filter = "txt|*.txt";
                    if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
                    {
                        using (StreamWriter sw = new StreamWriter(sfd.FileName))
                            sw.Write(Compute(pbImage.Image));

                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }

        private void cmdOpenImage_Click(object sender, EventArgs e)
        {
            try
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
                    if (ofd.ShowDialog() != DialogResult.Cancel)
                    {
                        txtImage.Text = Path.GetFileNameWithoutExtension(ofd.FileName);
                        this.pbImage.Image = Image.FromFile(ofd.FileName);
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }

        private void tbMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (tbMain.SelectedTab == tbPreview && pbImage.Image != null)
                {
                    txtPreview.Text = Compute(pbImage.Image);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }
        private void tbMain_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (tbMain.SelectedTab == tbPreview && pbImage.Image != null)
                {
                    txtPreview.Text = Compute(pbImage.Image);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.ToString());
            }
        }
        #endregion
    }
}
